package com.example.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.entity.Result;
import com.example.demo.entity.Audit;
import com.example.demo.mapper.AuditMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "审核访问接口")
@RestController
@CrossOrigin
@RequestMapping("/api/audit")
public class AuditController {
    @Autowired
    private AuditMapper auditmapper;

    @ApiOperation(value = "获取所有审核信息")
    @GetMapping("/info")
    public Result<?> selectAll(){
        List list = auditmapper.selectList(null);
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到审核信息！");
        }
    }

    @ApiOperation(value = "根据审核id获取审核信息")
    @GetMapping("/info/id")
    public Result<?> selectById(int id){
        Audit audit = auditmapper.selectById(id);
        if(audit != null){
            return Result.success(audit);
        } else {
            return Result.error("404", "找不到审核信息！");
        }
    }

    @ApiOperation(value = "根据审核文章id获取审核信息")
    @GetMapping("/info/arid")
    public Result<?> selectByArid(int arid){
        List list = auditmapper.selectList(Wrappers.<Audit>lambdaQuery().eq(Audit::getArid, arid));
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到审核信息！");
        }
    }

    @ApiOperation(value = "根据审核文章作者id获取审核信息")
    @GetMapping("/info/autid")
    public Result<?> selectByAutid(int autid){
        List list = auditmapper.selectList(Wrappers.<Audit>lambdaQuery().eq(Audit::getAutid, autid));
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到审核信息！");
        }
    }

    @ApiOperation(value = "根据审核人id获取审核信息")
    @GetMapping("/info/audid")
    public Result<?> selectByAudid(int audid){
        List list = auditmapper.selectList(Wrappers.<Audit>lambdaQuery().eq(Audit::getAudid, audid));
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到审核信息！");
        }
    }

    @ApiOperation(value = "根据审核结果获取审核信息")
    @GetMapping("/info/res")
    public Result<?> selectByRes(int res){
        List list = auditmapper.selectList(Wrappers.<Audit>lambdaQuery().eq(Audit::getRes, res));
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到审核信息！");
        }
    }

    @ApiOperation(value = "添加审核信息")
    @PostMapping("")
    public Result<?> insert(Audit audit) {
        /*if(){
            return Result.error("401", "参数不能为空！");
        }*/
        boolean bool = (auditmapper.insert(audit) > 0);
        if(bool) {
            return Result.success();
        } else {
            return Result.error("-1", "添加审核信息失败！");
        }
    }

    @ApiOperation(value = "根据审核id删除审核信息")
    @DeleteMapping("")
    public Result<?> delete(int id){
        if(id == 0) {
            return Result.error("401", "参数不能为空！");
        }
        boolean bool = (auditmapper.deleteById(id) > 0);
        if(bool) {
            return Result.success();
        } else {
            return Result.error("-1", "删除审核信息失败！");
        }
    }

    @ApiOperation(value = "根据审核id更新审核信息")
    @PutMapping("")
    public Result<?> update(Audit audit){
        if(audit.getId() == 0) {
            return Result.error("401", "参数不能为空！");
        }
        boolean bool = (auditmapper.updateById(audit) > 0);
        if(bool) {
            return Result.success();
        } else {
            return Result.error("-1", "更新审核信息失败！");
        }
    }
}